package com.shuqi.activity;

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import com.shuqi.activity.viewport.MainGroupBottomNav;
import com.shuqi.common.utils.Log4an;
import com.shuqi.controller.R;

/**
 * 主页框架 int 类型参数 position：0书架，1推荐
 * 
 * @author zhangpeng
 * 
 */
public class MainActivityGroup extends ActivityGroup {

    public static final int SHOW_UPDATE = 100; // 更新

    private static final int PAGE_POSITINO_OLD = -1; // 原始显示的位置

    public static final int PAGE_POSITION_BOOKSEARCH = 0; // 搜索

    public static final int PAGE_POSITION_BOOKSQUARE = 1; // 推荐

    public static final int PAGE_POSITION_RANKLIST = 2; // 排行

    public static final int PAGE_POSITION_SORT = 3; // 分类

    private MainGroupBottomNav bottom;

    public final static String INTENT_PAGE = "intent_page";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log4an.v("MainActivityGroup", "----onCreate---intent:" + getIntent().toString());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_maingroup);
        init();
    }

    private void init() {
        bottom = (MainGroupBottomNav) findViewById(R.id.act_maingroup_nav_mainbottom);
        bottom.setCurrentPage(getIntentOpenPage(getIntent()));
    }

    /**
     * 设置底部导航显隐
     * 
     * @param isShow
     */
    public void setMainGroupBottomNavVisible(boolean isShow) {
        bottom.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    /**
     * 设置当前底部显示的View
     * 
     * @param index
     */
    public void setCurView(int index) {
        bottom.setCurrentPage(index);
    }

    /** 获取intent中要打开的页面树荫 */
    private int getIntentOpenPage(Intent intent) {
        int page = PAGE_POSITION_BOOKSEARCH;
        if (intent != null) {
            page = intent.getIntExtra(INTENT_PAGE, PAGE_POSITION_BOOKSEARCH);
            if (page < PAGE_POSITION_BOOKSEARCH && page > PAGE_POSITION_SORT) {
                page = PAGE_POSITION_BOOKSEARCH;
            }
        }
        Log4an.i("MainActivityGroup", "intentPage = " + page);
        return page;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        Log4an.v("MainActivityGroup", "----onNewIntent---intent:" + intent.toString());
        setIntent(intent);
        int page = intent.getIntExtra(INTENT_PAGE, PAGE_POSITION_BOOKSEARCH);
        if (page == PAGE_POSITINO_OLD) {

        } else {
            setCurView(getIntentOpenPage(intent));
        }
    }

    /**
     * 
     * @param context
     * @param isClearTop
     */
    public static void show(Context context, boolean isClearTop) {
        Intent intent = new Intent(context, MainActivityGroup.class);
        intent.putExtra(INTENT_PAGE, PAGE_POSITINO_OLD);
        if (isClearTop) {
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        }
        context.startActivity(intent);
        if (context instanceof Activity) {
            // ((Activity) context).overridePendingTransition(R.anim.push_left_in,
            // R.anim.push_right_out);
            ((Activity) context).overridePendingTransition(R.anim.push_right_in,
                    R.anim.push_left_out);
        }
    }

    /**
     * 
     * @param context
     * @param page 0 表示书架 1 表示书城 2 表示排行 3 表示搜索
     * @param isClearTop
     */
    public static void show(Context context, int page, boolean isClearTop) {
        Intent intent = new Intent(context, MainActivityGroup.class);
        intent.putExtra(INTENT_PAGE, page);
        if (isClearTop) {
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        }
        context.startActivity(intent);
        if (context instanceof Activity) {
            ((Activity) context).overridePendingTransition(R.anim.push_right_in,
                    R.anim.push_left_out);
        }
    }

    /**
     * 
     * @param context
     * @param page 0 表示书架 1 表示书城 2 表示排行 3 表示搜索
     * @param isClearTop
     */
    public static void showFromBookShelf(Context context, int page, boolean isClearTop) {
        Intent intent = new Intent(context, MainActivityGroup.class);
        intent.putExtra(INTENT_PAGE, page);

        if (isClearTop) {
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        }
        context.startActivity(intent);
        if (context instanceof Activity) {
            ((Activity) context).overridePendingTransition(R.anim.push_right_in,
                    R.anim.push_left_out);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            BookShelfActivity.showFromMainActivityGroup(this, true);
            finish();
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() > 0) {
            event.startTracking();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        System.out.println("onkeylongpress");
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            openOptionsMenu();
        }
        return super.onKeyLongPress(keyCode, event);
    }
}
